import struct
import string

packer = struct.Struct('i 64s i i')

PORT = 10013

def trim_msg(msg):
	trimed = ''.join(c for c in msg if c in string.printable)
	return trimed

def auth(user,pasw) :
	user = user.strip()
	pasw = pasw.strip()
	f = open("userlist.txt", 'r')
	lines = f.readlines()
	for idx in xrange(len(lines)) :
		line = lines[idx]
		pos = str(line).find(" ")
	username = (str(line)[:pos]).strip()
	password = (str(line)[pos + 1:]).strip()
	if username == user and password == pasw :
		return True
	return False

class Card : 
	SPADE = 1
	HEART = 2
	DIAMOND = 3
	CLUB = 4

	type_dict = {SPADE : "Spade", HEART : "Heart", DIAMOND : "Diamond", CLUB : "Club"}
	point_dict = {1 : "A", 2 : "2", 3 : "3", 4 : "4", 5 : "5", 6 : "6", 7 : "7", 8 : "8", 9 : "9", 10 : "10", 11 : "J", 12 : "Q", 13 : "K"}

	def __init__(self, card_type, card_point) : 
		if card_type in self.type_dict : 
			self.card_type = card_type
		else : 
			raise Exception("Invalid Type Value.")

		if card_point in self.point_dict : 
			self.card_point = card_point
		else :
			raise Exception("Invalid Point Value.")

	def __str__(self) : 
		return self.type_dict[self.card_type] + " " + self.point_dict[self.card_point];


class Command :
	CMD_TERMINATE = -1
	CMD_ACK = 0
	CMD_NAMING = 1
	CMD_READY = 2
	CMD_START = 3
	CMD_BET_QUERY = 4
	CMD_BET_REPLY = 5
	CMD_ADD_QUERY = 6
	CMD_ADD_REPLY = 7
	CMD_GIVE_CARD = 8
	CMD_RESULT = 9
	CMD_MSG = 10
	CMD_MSG_GETCARD = 11
	CMD_UID = 12
	CMD_PLAYER_ENTER = 13
	CMD_AUTH_RESULT = 14

	def pack(self, *data) :
		return packer.pack(*data)

